﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class UsersDAO:IData
 	{
		public UsersDAO(String myconnecString) : base(myconnecString) { }

		public  int OnSubmit(Users item, int t) {
			 return connec.ExecuteNonQuery("sp_Users", CommandType.StoredProcedure,new SqlParameter("@flag", t),
				 new SqlParameter("@" + Users.c_id, item.id), new SqlParameter("@" + Users.c_userName, item.userName),
				 new SqlParameter("@" + Users.c_fullName, item.fullName), new SqlParameter("@" + Users.c_password, item.password),
				 new SqlParameter("@" + Users.c_email, item.email), new SqlParameter("@" + Users.c_create_on, item.create_on),
				 new SqlParameter("@" + Users.c_groupID, item.groupID), new SqlParameter("@" + Users.c_lastDate, item.lastDate),
				 new SqlParameter("@" + Users.c_status, item.status), new SqlParameter("@" + Users.c_passDate, item.passDate),
				 new SqlParameter("@" + Users.c_address, item.address), new SqlParameter("@" + Users.c_phone, item.phone),
				 new SqlParameter("@" + Users.c_birthDay, item.birthDay), new SqlParameter("@" + Users.c_isActive, item.isActive)); 
		}

		public  Users GetItem(IDataReader dr)
		{
			Users obj = new Users();
			obj.id = Convert.ToInt64(dr[Users.c_id]);
			obj.userName = Convert.ToString(dr[Users.c_userName]);
			obj.fullName = Convert.ToString(dr[Users.c_fullName]);
			obj.password = Convert.ToString(dr[Users.c_password]);
			obj.email = Convert.ToString(dr[Users.c_email]);
			obj.create_on = Convert.ToDateTime(dr[Users.c_create_on]);
			obj.groupID = Convert.ToInt32(dr[Users.c_groupID]);
			obj.lastDate = Convert.ToDateTime(dr[Users.c_lastDate]);
			obj.status = Convert.ToInt32(dr[Users.c_status]);
			obj.passDate = Convert.ToDateTime(dr[Users.c_passDate]);
			obj.address = Convert.ToString(dr[Users.c_address]);
			obj.phone = Convert.ToString(dr[Users.c_phone]);
			obj.birthDay = Convert.ToDateTime(dr[Users.c_birthDay]);
			obj.isActive = Convert.ToInt32(dr[Users.c_isActive]);
			return obj;
		}

		public  List<Users> GetAll()
		{
			return GetAll("select * from " + Users.b_Users, CommandType.Text);
		}

		public  Users GetItemById(object id)
		{
			 return this.GetAll().FirstOrDefault(x => x.id == (long)id);
		}

        private List<Users> GetAll(String strSQL, CommandType commandtype)
        {
            List<Users> list = new List<Users>();
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = commandtype;
            command.CommandText = strSQL;
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    list.Insert(0, GetItem(reader));
                }
                reader.Close();
            }
            connection.Close();
            return list;
        }
        public Users logIn(String userName,String password)
        {
            Users item = null;            
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "sp_login";
            SqlParameter param = new SqlParameter("@userName", SqlDbType.NVarChar);
            param.Value = userName;
            command.Parameters.Add(param);
            param = new SqlParameter("@password", SqlDbType.NVarChar);
            param.Value = password;
            command.Parameters.Add(param);
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    item = GetItem(reader);
                }
                reader.Close();
            }
            connection.Close();
            return item;
        }
	}

 }
